#include "KiraPlayerController.h"

KiraPlayerController::KiraPlayerController( btRigidBody *body, KiraCameraMan *cam ) : aircraft(body), camera(cam)
{
	minspeed = 10;
	maxspeed = 100;
	power = 1000000;
	airResistance = -power/maxspeed;
	aircraft->setLinearVelocity(btVector3(0,0,minspeed));
	speedUp = false;
	slowDown = false;
}

KiraPlayerController::~KiraPlayerController(void)
{
}

void KiraPlayerController::injectKeyDown( const OIS::KeyEvent& evt )
{
	if (evt.key == OIS::KC_UP)
	{
		speedUp = true;
	}
	else if (evt.key ==  OIS::KC_DOWN)
	{
		slowDown = true;
	}
}

void KiraPlayerController::injectKeyUp( const OIS::KeyEvent& evt )
{
	if (evt.key == OIS::KC_UP)
	{
		speedUp = false;
	}
	else if (evt.key ==  OIS::KC_DOWN)
	{
		slowDown = false;
	}
}

bool KiraPlayerController::frameRenderingQueued( const FrameEvent& evt )
{
	if (speedUp)
	{
		btVector3 v = aircraft->getLinearVelocity().normalized()*(power/aircraft->getLinearVelocity().length()+airResistance);
		aircraft->applyCentralForce(v);
	}
	else if (slowDown)
	{
		btVector3 v = aircraft->getLinearVelocity().normalized()*(-(power/(minspeed+maxspeed-aircraft->getLinearVelocity().length())+airResistance));
		aircraft->applyCentralForce(v);
	}
	else aircraft->clearForces();
	btVector3 v = aircraft->getTotalForce();
	//printf("v: %f, %f ,%f\n", v.x(), v.y(), v.z());
	return true;
}
